#include "Montion.h"
#include "mock/ArduinoMock.h"
#include "mock/MessageRouterMock.h"
#include "mock/LoggerMock.h"
using ::testing::_;
using ::testing::Return;
using ::testing::AnyNumber;

class MontionTest : public ::testing::Test {
public:
	MontionTest() : pin(33), REPORT_PERIOD(60) {}
protected:
	ArduinoMock arduino;
	MessageRouterMock router;
	int pin;
	const int REPORT_PERIOD;
	LoggerMock log;
};

TEST_F(MontionTest, noMontion){
	

	EXPECT_CALL(arduino, pin(pin, arduino.IN()));
	EXPECT_CALL(arduino, dWrite(pin, arduino.HIGHVAL()));
	Montion m(arduino, router, "kitchen", log, pin, REPORT_PERIOD);

	EXPECT_CALL(arduino, dRead(pin)).
		Times(3).
		WillRepeatedly(Return(arduino.LOWVAL()));
	m.tick(11000);
	m.tick(16000);
	m.tick(26000);


 //   EXPECT_CALL(router, postMessage(_, String("{\"event\":\"montion\", \"id\":\"kitchen\"}")))
//		.Times(1);

/*	EXPECT_CALL(arduino, dRead(pin)).
		Times(2).
		WillRepeatedly(Return(arduino.HIGHVAL()));
	m.tick(36000);
	m.tick(46000);*/
}

TEST_F(MontionTest, montion){	
	EXPECT_CALL(arduino, pin(pin, arduino.IN()));
	EXPECT_CALL(arduino, dWrite(pin, arduino.HIGHVAL()));
	Montion m(arduino, router, "kitchen", log, pin, REPORT_PERIOD);

	EXPECT_CALL(arduino, dRead(pin)).
		Times(1).
		WillRepeatedly(Return(arduino.HIGHVAL()));
    EXPECT_CALL(router, postMessage(_, String("{\"event\":\"montion\", \"id\":\"kitchen\"}")))
		.Times(1);
	m.tick(REPORT_PERIOD + 1);
}

TEST_F(MontionTest, reportMontionPeriodically){	
	EXPECT_CALL(arduino, pin(pin, arduino.IN()));
	EXPECT_CALL(arduino, dWrite(pin, arduino.HIGHVAL()));
	Montion m(arduino, router, "kitchen", log, pin, REPORT_PERIOD);

	EXPECT_CALL(arduino, dRead(pin)).
		WillRepeatedly(Return(arduino.HIGHVAL()));
    EXPECT_CALL(router, postMessage(_, String("{\"event\":\"montion\", \"id\":\"kitchen\"}")))
		.Times(1);
	m.tick(0);
	m.tick(REPORT_PERIOD);
	m.tick(REPORT_PERIOD+1);
}
